<template>
  <div class="app-container">
    <div class="hearder_bar">
      <el-input
        v-model="queryParams.templateName"
        placeholder="请输入流程名称"
        style="width: 250px; margin-bottom: 10px;"
        clearable
        @keyup.enter="handleQuery"
      >
        <template #append>
          <el-button icon="Search" @click="handleQuery"></el-button>
        </template>
      </el-input>
    </div>

    <el-table v-loading="loading" :data="configList" style="width: 100%" border>
      <el-table-column label="流程名称" prop="templateName" />
      <el-table-column label="版本" prop="status" />
      <el-table-column label="创建时间" prop="createTime" width="250">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最近一次修改时间" prop="updateTime" width="250">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button type="primary" link @click="handleView(scope.row)">查看</el-button>
          <el-button type="primary" link @click="handleEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      style="margin-top: 20px; justify-content: flex-end;"
      v-model:current-page="queryParams.pageNum"
      v-model:page-size="queryParams.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 流程详情弹窗 -->
    <ProcessDetailDialog 
      :model-value="dialogVisible" 
      @update:model-value="dialogVisible = $event"
      :process-data="currentProcessData" 
      @close="handleClose" 
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import ProcessDetailDialog from './components/ProcessDetailDialog.vue'
import {
  getApprovalConfigPage,
  getApprovalConfigDetail
} from '@/api/system/approval'

// 路由
const router = useRouter()

// 响应式数据
const loading = ref(true)
const total = ref(0)
const configList = ref([])
const dialogVisible = ref(false)
const currentProcessData = ref({
  templateName: '',
  status: '',
  createTime: '',
  updateTime: '',
  creatorName: '',
  processName: '',
  version: ''
})

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  templateName: null
})

/** 查询流程列表 */
const getApprovalConfig = () => {
  loading.value = true
  getApprovalConfigPage(queryParams).then(res => {
    loading.value = false
    configList.value = res?.list || [];
    total.value = res?.total || 0;
  }).catch(() => {
    // loading.value = false;
    configList.value = [];
    total.value = 0;
  });
}
// 分页大小改变
function handleSizeChange(val) {
  queryParams.pageSize = val
  queryParams.pageNum = 1
  getApprovalConfig()
}
// 当前页码改变
function handleCurrentChange(val) {
  queryParams.pageNum = val
  getApprovalConfig()
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1
  getApprovalConfig()
}

/** 查看按钮操作 */
const handleView = (row) => {
  // 获取审批配置详情
  getApprovalConfigDetail(row.templateId).then(res => {
    if (res.code === 200) {
      // 先设置数据，再打开弹窗
      currentProcessData.value = res.data || {}
      dialogVisible.value = true
    } else {
      ElMessage.warning('获取流程详情失败')
    }
  }).catch(() => {
    currentProcessData.value = {}
    ElMessage.error('获取流程详情失败')
  })
}

/** 关闭弹窗 */
const handleClose = () => {
  dialogVisible.value = false
  // 重置数据
  currentProcessData.value = {}
}

/** 编辑按钮操作 */
const handleEdit = (row) => {
  // 跳转到流程配置页面，携带流程ID参数
  router.push({
    path: `/system/approvals-config/approvals/${row.templateId}`, 
  })
}

/** 处理流程配置确认 */
const handleProcessConfigConfirm = (data) => {
  // 这里应该调用API保存流程配置
  console.log('保存流程配置:', data)
  ElMessage.success('流程配置保存成功')
  // 可以在这里刷新列表
  // getApprovalConfig()
}

// 组件挂载后执行
onMounted(() => {
  getApprovalConfig()
})
</script>

<style lang="scss" scoped>
.hearder_bar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 10px;
}
</style>
